<?php
	///////////////////////
	// GLOBAL PARAMETERS //
	///////////////////////
	
	$intSpaceBetweenWorkAndHomeEmailX = 150;
	$intSpaceBetweenWorkAndMobNumX = 85;
	$intLineSepBetFmlySize = 30;
	$intSmallLineSepSize = 11;
	$intLineSepSize = 15;
	$intFirstNameIndentX = 145;
	$intMinLeftPositionX = 50;
	$intLeftDetailsIndentX = 40;
	$intLeftPhoneDetailsIndentX = 90;
	$intLeftEmailDetailsIndentX = 190;
	$intIndvRoleIndentX = 210;
	$intHomeTelephoneIndentX = 400;
	$intLeftIndvIndentX = 80;
	$intMinSepAfterAddrY = -1;    // spacing between families

	///////////////////////
	//   PAGE SIZE (A4)  //
	///////////////////////
	$intPageSizeY = 595;  // ISO Standard A4 width - landscape
	$intPageSizeX = 842;  // ISO Standard A4 length - landscape
	$intTopOfPageY = 560; // maximum top of page to use
	$intTopOfPageX = 45;  // minimum left of page to use
	$intMinBottomPageY = 20;  // minimum bottom of page to use
	$maxFamilySize = 20;

	$intLineAfterFamilyAdjustment = 5;
	$intTitleAboveLineAdjustment = 5;


	///////////////////////
	// LINE CONSTRAINTS  //
	///////////////////////
	$intLineSpaceToLeaveAtBothEnds = 40;
	$intLineSepLeftX = $intLineSpaceToLeaveAtBothEnds;
	$intLineSepRightX = $intPageSizeX - $intLineSpaceToLeaveAtBothEnds-10; // 595 is A4 page width

	$intVerticalLineSepX = 4;


	$intSurnamePosX = $intTopOfPageX+5;
	$intFirstNamePosX = $intTopOfPageX+70;
	$intGenderPosX = $intTopOfPageX+135;
	$intRolePosX = $intTopOfPageX+167;
	$intAddr1PosX = $intTopOfPageX+230;
	$intSuburbPosX = $intTopOfPageX+320;
	$intPostcodePosX = $intTopOfPageX+395;
	$intHomePhonePosX = $intTopOfPageX+425;
	$intWorkPhonePosX = $intTopOfPageX+485;
	$intMobilePhonePosX = $intTopOfPageX+545;

	
	$bShowTitlePage = FALSE;
	$bShowPageNumber = FALSE;

	
	
	
	//////////////////////////////////////////////////////
	// GPAC Database is Copyright 2002 Colin Todd       //
	// Written by Colin Todd - All rights reserved.     //
	//                                                  //
	// colintodd@myfastmail.com                         //
	//////////////////////////////////////////////////////


		//check user is logged in
//		include_once('_includes/master_include.php');
		$root = '../';
		include('../include/dbconnect.php');
		include($root .'include/config.php');
		include($root.'include/functions.php');


		$Indv_sql = html_entity_decode(@$_GET['indv_sql']);
		$Indv_sql = urldecode($Indv_sql);

		$Fmly_sql = html_entity_decode(@$_GET['fmly_sql']);
		$Fmly_sql  = urldecode($Fmly_sql);

		$bShowTitlePage = @$_GET['ShowTitlePage'];
		$bShowPageNumber = @$_GET['ShowPageNum'];
		$intPageCount = @$_GET['StartPageNum']-1; // set count to 0


//echo "Fmly_sql  = $Fmly_sql <p>";
//echo "Indv_sql  = $Indv_sql <p>";
//exit;
		$sql = "SELECT * FROM (((Individual INNER JOIN Family ON Individual.FamilyId = Family.FamilyId) LEFT JOIN Gender ON Individual.GenderCode = Gender.GenderCode) LEFT  JOIN Title ON Individual.Title = Title.Title) LEFT  JOIN RoleType ON Individual.RoleTypeCode = RoleType.RoleTypeCode WHERE ".$Indv_sql." AND ".$Fmly_sql." ORDER BY Surname;";

	//  $sql = "SELECT * FROM (((Individual) LEFT JOIN Gender ON Individual.GenderCode = Gender.GenderCode) LEFT  JOIN Title ON Individual.Title = Title.Title) LEFT  JOIN RoleType ON Individual.RoleTypeCode = RoleType.RoleTypeCode  ORDER BY Individual.RoleTypeCode DESC, Individual.GenderCode DESC;";



	//     echo "<p>$sql<p>";
	   $result = mysql_query($sql);
		$resultsnumber = 0;
	   $resultsnumber = @mysql_numrows($result);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	   

	$pdf = pdf_new();
	if (!pdf_open_file($pdf, "")) {
		print error;
		exit;
	};
	pdf_set_info($pdf, "Author", "Colin Todd");
	pdf_set_info($pdf, "Title", "Spreadsheet");
	pdf_set_info($pdf, "Creator", "Colin Todd");
	pdf_set_info($pdf, "Subject", "GPAC Database");
	// get and use a font object

//	$font1_normal = pdf_findfont($pdf, "Arial", "host", 1); 
//	$font = pdf_findfont($pdf, "Times New Roman", "winansi", 1);
//	$courier = pdf_findfont($pdf, "Courier", "host", 1); 
	$Helvetica  = PDF_findfont($pdf,"Helvetica","host",0);
	$font1_normal = $Helvetica  ;
	$font1_normal_bold  = PDF_findfont($pdf,"Helvetica-Bold","host",0);

//	PDF_set_parameter($pdf, "openaction", "fitwidth");
	PDF_set_parameter($pdf, "openaction", "fitpage");
	PDF_set_parameter($pdf, "openmode", "none");

	// initialise variables for start
	$intPageCount = 0; // set count to 0
	$y = $intMinBottomPageY-1 ; // set over limit so that new page (page1) will be inserted
	$x = $intTopOfPageX;

//	$top_lineY = 0;
//	$bot_lineY = 0;


	//pdf_setfont($pdf, $font1_normal, 20);
	//pdf_show_xy($pdf, "Number of results : $resultsnumber", $x, $y+15);

	



	if($resultsnumber!=0) {
		
	
		while ($row = mysql_fetch_array($result)) { 
	//		$y = $y - 5; // a small separator

			//
			// Grab Family details
			//



	//	   $Indv_FamilyShortName = getFormattedFamilyShortName($FamilyId," ");
	//	   $Fmly_ResAddress  = getFormattedResidentialAddress($FamilyId, chr(10),FALSE,FALSE,FALSE);
	//	   $Fmly_HomePhone     = getFormattedHomePhoneNumber($FamilyId," ",FALSE, TRUE);
	//	   $Fmly_Photo = $row["FamilyPhoto"];


			if (($y-$maxFamilySize)<$intMinBottomPageY){
				// insert new pagge
				// reset top values
				if($intPageCount == 0) 
				{
						// insert titlepage
						if($bShowTitlePage ) {
							if(trim($Indv_sql)=="TRUE") {
								$Indv_sql_ = "n/a";
							}else {
								$pos = strspn($Indv_sql, "TRUE   AND");
								$Indv_sql_ = substr($Indv_sql,$pos);
							}
							if(trim($Fmly_sql)=="TRUE") {
								$Fmly_sql_ = "n/a";
							}else {
	//							$Fmly_sql_ = $Fmly_sql;
								$pos = strspn ($Fmly_sql, "TRUE   AND");
								$Fmly_sql_ = substr($Fmly_sql,$pos);
							}
							// insert titlepage
							printPDFTitlePage($pdf, "Indv Report", "Filtered by : ".$Indv_sql." and ".$Fmly_sql, $intPageSizeX , $intPageSizeY, $intTopOfPageX, $intTopOfPageY,$intMinBottomPageY, $font1_normal,$font1_normal_bold );
							$intPageCount++;

						}
					// don't end page
				} else
				{

					$top_lineY = $intTopOfPageY-$intLineAfterFamilyAdjustment;
					$bot_lineY = $y + $intLineSepSize - $intLineAfterFamilyAdjustment;

					// draw vertical lines
					//Left
					PDF_moveto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$top_lineY );
					PDF_lineto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					//Right
					PDF_moveto($pdf,$intLineSepRightX ,$top_lineY);
					PDF_lineto($pdf,$intLineSepRightX ,$bot_lineY );
					PDF_stroke($pdf);
					//Column Uprights
					PDF_moveto($pdf,$intFirstNamePosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intFirstNamePosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intGenderPosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intGenderPosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intRolePosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intRolePosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intAddr1PosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intAddr1PosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intSuburbPosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intSuburbPosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intPostcodePosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intPostcodePosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intHomePhonePosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intHomePhonePosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intWorkPhonePosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intWorkPhonePosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					PDF_moveto($pdf,$intMobilePhonePosX - $intVerticalLineSepX ,$top_lineY);
					PDF_lineto($pdf,$intMobilePhonePosX - $intVerticalLineSepX ,$bot_lineY );
					PDF_stroke($pdf);
					
					// Left footer
					pdf_show_xy($pdf, "Full Individual Listing", $intTopOfPageX, $intMinBottomPageY);
					// Middle footer
					pdf_show_xy($pdf, "Page $intPageCount", $intPageSizeX/2-($intTopOfPageX), $intMinBottomPageY);
					// Right footer
					pdf_show_xy($pdf, date("g:i a F j, Y"), ($intPageSizeX-$intTopOfPageX)-100, $intMinBottomPageY);

					pdf_end_page($pdf);
		
				}
				// start a new page (A4)
				pdf_begin_page($pdf, $intPageSizeX , $intPageSizeY );
				$intPageCount++;
//				pdf_add_outline($pdf, "Page $intPageCount");
				pdf_setfont($pdf, $font1_normal, 10);
				// reset X,Y values to defaults
				$y = $intTopOfPageY;
				$x = $intTopOfPageX;

				// test coordinates
				pdf_setfont($pdf, $font1_normal, 8);
//				pdf_show_xy($pdf, "$x..$y", $x-40, $y);

				pdf_setfont($pdf, $font1_normal_bold, 8);
				// insert new top line to page
				pdf_show_xy($pdf, "Surname", $intSurnamePosX , $y );
				pdf_show_xy($pdf, "FirstName", $intFirstNamePosX , $y);
				pdf_show_xy($pdf, "Role", $intRolePosX , $y);
				pdf_show_xy($pdf, "Sex", $intGenderPosX , $y);
				pdf_show_xy($pdf, "Address", $intAddr1PosX , $y);
				pdf_show_xy($pdf, "Suburb", $intSuburbPosX , $y);
				pdf_show_xy($pdf, "Pcode", $intPostcodePosX , $y);
				pdf_show_xy($pdf, "HomePhone", $intHomePhonePosX , $y);
				pdf_show_xy($pdf, "WorkPhone", $intWorkPhonePosX , $y);
				pdf_show_xy($pdf, "MobilePhone", $intMobilePhonePosX , $y);

			
				PDF_moveto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$y - $intLineAfterFamilyAdjustment );
				PDF_lineto($pdf,$intLineSepRightX ,$y - $intLineAfterFamilyAdjustment );
				PDF_stroke($pdf);

				$y = $y - $intLineSepSize;
			}
			else
			{
				// then OK to print
				// $y = $y - $intLineSepSize;
			}

			pdf_setfont($pdf, $font1_normal, 8);
//			pdf_show_xy($pdf, "$x..$y", $x-40, $y);

		   $FamilyId = $row["FamilyId"];
		   $IndividualId = $row["IndividualId"];
		   $FirstName = $row["FirstName"];
		   $Surname = $row["Surname"];
		   $Role = $row["RoleTypeCode"];
		   $Gender = $row["GenderCode"];
		   $AddressLine1 = getFormattedResidentialAddress($FamilyId, "\n", TRUE, FALSE, TRUE, TRUE);
		   $Suburb= $row["ResidentialSuburb"];
		   $Postcode = $row["ResidentialPostcode"];
		   $HomePhone =  getFormattedHomePhoneNumber($FamilyId, " ", FALSE, FALSE, FALSE, "PRIVATE");
		   $WorkPhone = getFormattedWorkPhoneNumber($IndividualId,    " ", FALSE, FALSE, FALSE, "PRIVATE");
		   $MobilePhone = getFormattedMobilePhoneNumber($IndividualId," ", FALSE, FALSE,  "PRIVATE");

			pdf_setfont($pdf, $font1_normal, 8);
			pdf_show_xy($pdf, $Surname, $intSurnamePosX , $y);
			pdf_show_xy($pdf, $FirstName, $intFirstNamePosX , $y);
			pdf_add_outline($pdf, "$FirstName $Surname");
			pdf_show_xy($pdf, $Role, $intRolePosX , $y);
			pdf_show_xy($pdf, $Gender, $intGenderPosX , $y);

//		$nr = pdf_show_boxed ( $pdf, $sql, 50 , 200, 700, 300, "left" );
//		$nr = pdf_show_boxed ( $pdf, $AddressLine1, $intAddr1PosX, $y, 100, 50,"justify");

		$text = $AddressLine1;
		//$text = "fajshfkjahsflkjhasfklhsadlkfjhaslkdfhsalk\nfsakdhfkjashdfkajh\nsdfasdasd";
		$fontsize = pdf_get_value($pdf,'fontsize'); 
		$length = pdf_stringwidth($pdf,$text); 
		$textHeight = 1; 
		$height_change = 0;

		while(pdf_show_boxed($pdf,$text,  $intAddr1PosX, $y,100,$textHeight,"justify",'blind') > 0) {
			$textHeight += $fontsize; 
			$height_change +=1;
		}
		if($height_change==0) {
			$height_change=10;
		}
//		$textHeight = $height_change;
//		pdf_show_xy($pdf, "textHeight = ".$textHeight." ".$height_change , $intMobilePhonePosX+120 , $y);
//		pdf_show_boxed($pdf,$text, $intAddr1PosX, $y-($textHeight/$height_change-1) ,100,$textHeight,"justify"); 


			pdf_show_xy($pdf, $AddressLine1, $intAddr1PosX , $y);

			pdf_show_xy($pdf, $Suburb, $intSuburbPosX , $y);
			pdf_show_xy($pdf, $Postcode, $intPostcodePosX , $y);
			pdf_show_xy($pdf, $HomePhone, $intHomePhonePosX , $y);
			pdf_show_xy($pdf, $WorkPhone, $intWorkPhonePosX , $y);
			pdf_show_xy($pdf, $MobilePhone, $intMobilePhonePosX , $y);
				

	//		pdf_show_xy($pdf, "".strtoupper($Indv_FamilyShortName)."", $x, $y);
	//		pdf_show_xy($pdf, "".$Fmly_HomePhone."", $x+$intHomeTelephoneIndentX, $y);

			pdf_setfont($pdf, $font1_normal, 10);
			$intTopOfAddressY = $y-$textHeight/2;
//			$intTopOfAddressY = $y-$intLineSepSize;

			//
			// Insert line at end of Family
			//
//			if( ($y-$textHeight/2) >  ($y - $intLineAfterFamilyAdjustment) ){
				PDF_moveto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$y - $intLineAfterFamilyAdjustment );
				PDF_lineto($pdf,$intLineSepRightX ,$y - $intLineAfterFamilyAdjustment );
				$y = $y - $intLineSepSize;
//			} else {
//				PDF_moveto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$y-$textHeight/2 );
//				PDF_lineto($pdf,$intLineSepRightX ,$y-$textHeight/2 );
//				$y = $y-($textHeight/2) - $intLineSepSize;
//			}
//			PDF_moveto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$y - $intLineAfterFamilyAdjustment );
//			PDF_lineto($pdf,$intLineSepRightX ,$y - $intLineAfterFamilyAdjustment );
			PDF_stroke($pdf);
		
			//pdf_show_xy($pdf, "$x..$y", $x-10, $y);

		} // end while family
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		//pdf_setfont($pdf, $font, 12); // font size 12

		// add an image under the text
	//	$image = pdf_open_image_file($pdf, "gif", "../include/logoB.gif");
		//pdf_place_image($pdf, $image, 50, 650, 0.25);


		/*
		$y = 800;
		$x = 40;
		while ($x < 300){
			pdf_show_xy($pdf, "Colin ($x, $y)", $x, $y);
			$x = $x + 80;
		}
		$x = 50;
		$y = 720;
		while ($y > 300){
			pdf_show_xy($pdf, "Todd ($x, $y)", $x, $y);
			pdf_place_image($pdf, $image, $x+100, $y, .5); // last arg is scalling facter. 1 = 1:1

			$y = $y - 50;
		}


		//pdf_show_xy($pdf, "Nedxt line 1 2 3 4 5 6 7 8 9", 1, 1);
		pdf_moveto($pdf, 50, 740);
		pdf_lineto($pdf, 330, 740);
		pdf_stroke($pdf);
		*/

		// set a colour for the line
		//pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

		// place a logo in the top left corner
		//$image = pdf_open_image_file($pdf, "gif", "include/logo.gif");
		//pdf_place_image($pdf, $image, 50, 785, 0.5);

		// draw a line under the logo
		//pdf_moveto($pdf, 20, 780);
		//pdf_lineto($pdf, 575, 780);
		//pdf_stroke($pdf);

		// draw another line near the bottom of the page pdf_moveto($pdf, 20, 50); 
		//pdf_lineto($pdf, 575, 50); 
		//pdf_stroke($pdf);

		// and write some text under it
		//pdf_show_xy($pdf, "Confidential and proprietary", 200, 35);
		pdf_set_value($pdf, "textrendering", 0);

		// draw vertical lines
		$top_lineY = $intTopOfPageY-$intLineAfterFamilyAdjustment;
		$bot_lineY = $y + $intLineSepSize - $intLineAfterFamilyAdjustment;
		//Left
		PDF_moveto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$top_lineY );
		PDF_lineto($pdf,$intSurnamePosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		//Right
		PDF_moveto($pdf,$intLineSepRightX ,$top_lineY);
		PDF_lineto($pdf,$intLineSepRightX ,$bot_lineY );
		PDF_stroke($pdf);
		//Column Uprights
		PDF_moveto($pdf,$intFirstNamePosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intFirstNamePosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intGenderPosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intGenderPosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intRolePosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intRolePosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intAddr1PosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intAddr1PosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intSuburbPosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intSuburbPosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intPostcodePosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intPostcodePosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intHomePhonePosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intHomePhonePosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intWorkPhonePosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intWorkPhonePosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);
		PDF_moveto($pdf,$intMobilePhonePosX - $intVerticalLineSepX ,$top_lineY);
		PDF_lineto($pdf,$intMobilePhonePosX - $intVerticalLineSepX ,$bot_lineY );
		PDF_stroke($pdf);


	} else {
		// contains no data
		pdf_begin_page($pdf, $intPageSizeX , $intPageSizeY );
		$intPageCount++;
		pdf_add_outline($pdf, "Page $intPageCount");
		// reset X,Y values to defaults
		$y = $intTopOfPageY;
		$x = $intTopOfPageX;
		pdf_setfont($pdf, $font1_normal_bold, 20);
		// insert new top line to page
		pdf_show_xy($pdf, "Error! Recordset contains no data", $intSurnamePosX , $y );
		pdf_setfont($pdf, $font1_normal, 10);
//		pdf_show_xy($pdf, $sql, $intSurnamePosX , $y-100 );
		$nr = pdf_show_boxed ( $pdf, $sql, 50 , 200, 700, 300, "left" );
//		$nr = pdf_show_boxed(  $pdf, $Fmly_ResAddress, $x+10, $intTopOfAddressY , 100, 80, "left");

	}
	pdf_setfont($pdf, $font1_normal, 10);
//	$intTopOfAddressY = $y-$intLineSepSize;

	// Left footer
	pdf_show_xy($pdf, "Full Individual Listing", $intTopOfPageX, $intMinBottomPageY);
	// Middle footer
	pdf_show_xy($pdf, "Page $intPageCount", $intPageSizeX/2-($intTopOfPageX), $intMinBottomPageY);
	// Right footer
	pdf_show_xy($pdf, date("g:i a F j, Y"), ($intPageSizeX-$intTopOfPageX)-100, $intMinBottomPageY);



	pdf_end_page($pdf);
	pdf_close($pdf);
	//pdf_delete($pdf);

	$buf = pdf_get_buffer($pdf);
	$len = strlen($buf);



	header("Content-type: application/pdf");
	header("Content-Length: $len");
	header("Content-Disposition: inline; filename=foo.pdf");
	print $buf;

	pdf_delete($pdf);

?>